package kr.co.happenstance;



import kr.co.happenstance.util.AndroidUtil;

import android.app.*;
import android.app.AlertDialog.Builder;
import android.content.*;
import android.content.DialogInterface.OnClickListener;
import android.graphics.drawable.*;
import android.view.*;
import android.widget.*;

/**
 * AlertDialog 의 title, messgae, 버튼의 폰트 변경을 위한 커스텀 Builder 클래스
 * 
 * @author Administrator
 *
 */
public class CustomAlertDialogBuilder extends Builder{
	@SuppressWarnings("unused")
	private String TAG = "<<<CustomAlertDialogBuilder>>>";
	
	private TextView title = null;
	private TextView message = null;
	private ImageView icon = null;
	
	/**
	 * 생성자
	 * 
	 * @param context
	 */
	public CustomAlertDialogBuilder(Context context){
		this(context, null, null, -1, true);
	}
	
	/**
	 * 생성자
	 * 
	 * @param context
	 * @param _title
	 * @param _message
	 */
	public CustomAlertDialogBuilder(Context context, String _title, String _message){
		this(context, _title, _message, -1, true);
	}
	
	/**
	 * 생성자
	 * 
	 * @param context
	 * @param title_id
	 * @param message_id
	 */
	public CustomAlertDialogBuilder(Context context, int title_id, int message_id){
		this(context, context.getString(title_id), context.getString(message_id), -1, true);
	}
	
	/**
	 * 생성자
	 * 
	 * @param context
	 * @param title_id
	 * @param message_id
	 * @param _iconId
	 * @param _cancelable
	 */
	public CustomAlertDialogBuilder(Context context,  int title_id, int message_id, int _iconId, boolean _cancelable){
		this(context, context.getString(title_id), context.getString(message_id), _iconId, _cancelable);
	}
	
	/**
	 * 생성자
	 * 
	 * @param context
	 * @param _title
	 * @param _message
	 * @param _iconId
	 * @param _cancelable
	 */
	public CustomAlertDialogBuilder(Context context, String _title, String _message, int _iconId, boolean _cancelable){
		super(context);
		
		View customTitle = View.inflate(context, R.layout.alert_dialog_title, null);
		title = (TextView) customTitle.findViewById(R.id.alertTitle);
		AndroidUtil.setTextViewAppFontTypeface(title);
		
		
		icon = (ImageView) customTitle.findViewById(R.id.icon);
		setCustomTitle(customTitle);
		
		View customMessage = View.inflate(context,R.layout.alert_dialog_message, null);
		message = (TextView) customMessage.findViewById(R.id.message);
		AndroidUtil.setTextViewAppFontTypeface(message);
		setView(customMessage);
		
		if(_title != null){
			setTitle(_title);
		}
		
		if(_message != null){
			setMessage(_message);
		}
		
		if(_iconId != -1){
			setIcon(_iconId);
		}
		
		setCancelable(_cancelable);
		
		
		//기본 버튼 클릭 리스너 달아주기(확인버튼,  확인 버튼 클릭시 창닫는 리스너)
		CustomAlertDialogBuilder.this.setPositiveButton(R.string.confirm, new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
			}
		});
		
		
	}
	
	@Override
	public Builder setTitle(CharSequence title) {
		this.title.setText(title);
		return this;
	}

	@Override
	public Builder setTitle(int titleId) {
		this.title.setText(titleId);
		return this;
	}

	@Override
	public Builder setMessage(CharSequence message) {
		this.message.setText(message);
		return this;
	}

	@Override
	public Builder setMessage(int messageId) {
		this.message.setText(messageId);
		return this;
	}

	@Override
	public Builder setIcon(Drawable icon) {
		this.icon.setImageDrawable(icon);
		return this;
	}

	@Override
	public Builder setIcon(int iconId) {
		return super.setIcon(iconId);
	}
	
	@Override
	public Builder setCancelable(boolean cancelable){
		return super.setCancelable(cancelable);
	}
	
	@Override
	public Builder setAdapter(ListAdapter adapter, OnClickListener listener) {
		return super.setAdapter(adapter, listener);
		
	}
	
	/**
	 * 버튼 Text를 App Font Typeface로 변경후 리턴
	 * 
	 * @return 
	 * 
	 */
	@Override
	public AlertDialog create() {
		AlertDialog ad = super.create();
		
		//show 메소드 호출을 해줘야 button들을 가져올수 있음 (이거 안해주면 button들이 모두 null값이 넘어옴)
		ad.show();
		
		Button negativeButton = ad.getButton(AlertDialog.BUTTON_NEGATIVE);
		if(negativeButton != null){
			negativeButton.setTypeface(AndroidUtil.getAppFontTypeFace());
		}
		
		Button neutralButton = ad.getButton(AlertDialog.BUTTON_NEUTRAL);
		if(neutralButton != null){
			neutralButton.setTypeface(AndroidUtil.getAppFontTypeFace());
		}
		
		Button positiveButton = ad.getButton(AlertDialog.BUTTON_POSITIVE);
		if(positiveButton != null){
			positiveButton.setTypeface(AndroidUtil.getAppFontTypeFace());

		}
		return ad;
		
		
	}
	
	
	
	

}
